<template>
  <uni-popup ref="popupRef" type="dialog" :mask-click="maskClick">
    <view class="read-dialog-wrapper">
      <view class="read-dialog-header">
        <text class="read-dialog-title">{{ title }}</text>
      </view>
      <view class="read-dialog-content">
        <scroll-view scroll-y class="read-scroll-view">
          <slot name="content">
            <view class="read-text">
              {{ content }}
            </view>
          </slot>
        </scroll-view>
        <view v-if="showTipArea" class="read-tip">
          <text v-if="showCountdown" class="countdown-text">{{ formattedTipText }}</text>
          <text v-else class="ready-text">{{ readyText }}</text>
        </view>
      </view>
      <view class="read-dialog-footer">
        <view v-if="showCancel" class="read-dialog-btn cancel-btn" @click="handleCancel">
          {{ cancelText }}
        </view>
        <view
          class="read-dialog-btn confirm-btn"
          :class="{ disabled: showCountdown }"
          @click="handleConfirm">
          {{ confirmLabel }}
        </view>
      </view>
    </view>
  </uni-popup>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue';

const emit = defineEmits<{
  (event: 'confirm'): void;
  (event: 'cancel'): void;
}>();

const props = withDefaults(
  defineProps<{
    title?: string;
    content?: string;
    countdown?: number;
    tipTemplate?: string;
    readyText?: string;
    confirmText?: string;
    cancelText?: string;
    maskClick?: boolean;
    showTipArea?: boolean;
    showCancel?: boolean;
  }>(),
  {
    title: '请仔细阅读',
    content: '',
    countdown: 0,
    tipTemplate: '请仔细阅读，{countdown}秒后可以提交',
    readyText: '✓ 已阅读完成',
    confirmText: '确认',
    cancelText: '取消',
    maskClick: false,
    showTipArea: true,
    showCancel: true,
  }
);

const popupRef = ref<any>(null);
const safeCountdown = computed(() => Math.max(Number(props.countdown) || 0, 0));
const showCountdown = computed(() => safeCountdown.value > 0);
const formattedTipText = computed(() => props.tipTemplate.replace('{countdown}', String(safeCountdown.value)));
const confirmLabel = computed(() =>
  showCountdown.value ? `${props.confirmText}(${safeCountdown.value}s)` : props.confirmText
);

const handleConfirm = () => {
  emit('confirm');
};

const handleCancel = () => {
  emit('cancel');
};

const open = () => {
  popupRef.value?.open();
};

const close = () => {
  popupRef.value?.close();
};

defineExpose({
  open,
  close,
});
</script>

<style scoped lang="scss">
.read-dialog-wrapper {
  width: 600rpx;
  background: #fff;
  border-radius: 20rpx;
  overflow: hidden;
}

.read-dialog-header {
  padding: 30rpx;
  text-align: center;
  border-bottom: 1px solid #f0f0f0;
}

.read-dialog-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.read-dialog-content {
  padding: 30rpx;
  max-height: 500rpx;
}

.read-scroll-view {
  max-height: 350rpx;
  margin-bottom: 20rpx;
}

.read-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.8;
  white-space: pre-wrap;
}

.read-tip {
  text-align: center;
  padding-top: 20rpx;
  border-top: 1px solid #f0f0f0;
}

.countdown-text {
  font-size: 26rpx;
  color: #ff6b35;
}

.ready-text {
  font-size: 26rpx;
  color: #ffa500;
}

.read-dialog-footer {
  display: flex;
  border-top: 1px solid #f0f0f0;
}

.read-dialog-btn {
  flex: 1;
  height: 88rpx;
  line-height: 88rpx;
  text-align: center;
  font-size: 32rpx;
  border-right: 1px solid #f0f0f0;

  &:last-child {
    border-right: none;
  }
}

.cancel-btn {
  color: #666;
  background: #fff;
}

.confirm-btn {
  color: #fff;
  background: #d92121;

  &.disabled {
    background: #ccc;
    color: #999;
    cursor: not-allowed;
  }
}
</style>

